#ifndef _TEST_JSON_
#define _TEST_JSON_

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <string>
#include <mutex>
#include "server_config.hpp"

#define FILEPATH "../config/server.json"

class ServerConfigSingleton
{
private:
    ServerConfigSingleton() = default;
    ServerConfigSingleton(const ServerConfigSingleton &) = delete;
    ServerConfigSingleton operator=(const ServerConfigSingleton &) = delete;

public:
    ~ServerConfigSingleton()
    {
        delete instance;
    }

private:
    static ServerConfigSingleton *instance;
    static std::mutex mutex;

private:
    static boost::property_tree::ptree pt;

public:
    template <typename T>
    static T getValue(std::string name)
    {
        return pt.get<T>(name);
    }
    static ServerConfigSingleton *getInstance()
    {
        if (instance == nullptr)
        {
            std::lock_guard<std::mutex> lock(mutex);
            if (instance == nullptr)
            {
                instance = new ServerConfigSingleton();
                boost::property_tree::json_parser::read_json(FILEPATH, pt);
            }
        }
        return instance;
    }
};

// 初始化静态成员变量
ServerConfigSingleton *ServerConfigSingleton::instance = nullptr;
std::mutex ServerConfigSingleton::mutex;
boost::property_tree::ptree ServerConfigSingleton::pt;

#endif